package com.ailanyin.admin.modules.system.controller;

import com.ailanyin.admin.modules.system.model.SysNotice;
import com.ailanyin.admin.modules.system.service.SysNoticeService;
import com.ailanyin.common.result.AjaxResult;
import com.ailanyin.common.result.CommonPage;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统公告管理
 *
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/15 0015 下午 14:41
 */
@RestController
@RequestMapping("/system/notice")
@Api(tags = "系统公告管理")
public class SysNoticeController {

    @Autowired
    private SysNoticeService noticeService;

    @PreAuthorize("hasAuthority('system:notice:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取通知公告列表")
    public CommonPage list(SysNotice notice,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return CommonPage.restPage(list);
    }

    @PreAuthorize("hasAuthority('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    @ApiOperation(value = "根据通知公告编号获取详细信息")
    public AjaxResult getInfo(@PathVariable Long noticeId) {
        return AjaxResult.success(noticeService.selectNoticeById(noticeId));
    }

    @PreAuthorize("hasAuthority('system:notice:add')")
    @PostMapping
    @ApiOperation(value = "新增通知公告")
    public AjaxResult add(@Validated @RequestBody SysNotice notice) {
        return AjaxResult.toResult(noticeService.insertNotice(notice));
    }

    @PreAuthorize("hasAuthority('system:notice:edit')")
    @PutMapping
    @ApiOperation(value = "修改通知公告")
    public AjaxResult edit(@Validated @RequestBody SysNotice notice) {
        return AjaxResult.toResult(noticeService.updateNotice(notice));
    }

    @PreAuthorize("hasAuthority('system:notice:remove')")
    @DeleteMapping("/{noticeIds}")
    @ApiOperation(value = "删除通知公告")
    public AjaxResult remove(@PathVariable Long[] noticeIds) {
        return AjaxResult.toResult(noticeService.deleteNoticeByIds(noticeIds));
    }
}
